import { debounce } from "../Z";

export const reactDndTypes = {
    ROW: 'row',
    COL: "col",
    BUTTON: 'button',
    INPUI: 'input',
}

export const acceptTypes = Object.values(reactDndTypes).filter(it => it !== "col");

export const HoverPositionType = {
    TOP: 'top',
    BOTTOM: 'bottom',
    MIDDLE: 'middle'
}
// 查询所有的数据，根据索引
export const findAllChildrenWithIndex = (indexTable: { [x: string]: any; }, parentId: string | number, includeSelf = false) => {
    const result = [];
    const queue = [];
    let head = 0; // 队列头指针

    if (includeSelf) {
        result.push(parentId);
        queue.push(parentId);
    } else {
        queue.push(...(indexTable[parentId] || []));
    }

    while (head < queue.length) {
        const currentId:any= queue[head] ; // 获取当前元素但不移除
        head++; // 移动头指针

        result.push(currentId);
        queue.push(...(indexTable[currentId] || []));
    }

    return result;
}
export const deleteNodeAndChildren = (boxData: any, boxIndex: { [x: string]: any; }, Id: string) => {
    const toDeleteIds = findAllChildrenWithIndex(boxIndex, Id, true);
    const toDeleteSet = new Set(toDeleteIds);
    const newIndexTable = { ...boxIndex };
    const newDataTable = { ...boxData };
    for (const deletedId of toDeleteIds) {
        delete newIndexTable[deletedId];
        delete newDataTable[deletedId];
    }
    for (const [parentId, children] of Object.entries(newIndexTable)) {
        if (Array.isArray(children)) {
            newIndexTable[parentId] = children.filter(childId => !toDeleteSet.has(childId));
        }
    }
    return {
        newDataTable,
        newIndexTable
    };
}

